package com.iseastar.dianxiaosan.home;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

import com.iseastar.BaseActivity2;
import com.iseastar.dianxiaosan.home.adapter.TradeRecordAdapter;
import com.iseastar.dianxiaosan.model.TradeRecordResultBean;
import com.iseastar.guojiang.app.AppCache;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.ReqResult;
import com.iseastar.guojiang.model.UserBean;
import com.shouhuobao.station.R;

import droid.frame.view.SwipeListViewOnScrollListener;
import droid.frame.view.xlist.SListView;

/**
 * 店小三 交接记录
 * 
 * @author HuXinDan<br>
 *         2016-11-18上午10:54:45
 */
public class TradeRecordActivity extends BaseActivity2 {

	private SListView mListView;
	private TradeRecordAdapter mAdapter;

	private TextView mTradeRecordNum;
	private TextView mTradeRecordCount;
	private TextView mTradeRecordContent;

	protected void onCreate(Bundle savedInstanceState) {
		UserBean user = AppCache.getUser();
		super.onCreate(savedInstanceState);
		AppHttp.getInstance().stationTradeRecord(user.getStationId(), 0);
	}

	protected void findViewById() {
		setContentView(R.layout.trade_record);
		super.findViewById();
		getAppTitle().setCommonTitle("交接记录");

		mListView = (SListView) findViewById(R.id.listview);
		mTradeRecordNum = (TextView) findViewById(R.id.trade_record_num);// 交易数量
		mTradeRecordCount = (TextView) findViewById(R.id.trade_record_count);// 交易数量
		mTradeRecordContent = (TextView) findViewById(R.id.trade_record_content);// 交易次数
		mListView.setFooterDividersEnabled(true);
		
		mSwipeLayout.setOnRefreshListener(new OnRefreshListener() {
			@Override
			public void onRefresh() {
				UserBean user = AppCache.getUser();
				AppHttp.getInstance().stationTradeRecord(user.getStationId(), 0);
			}
		});
		mListView.setOnScrollListener(new SwipeListViewOnScrollListener(mSwipeLayout));
	}

	@Override
	public boolean handleMessage(Message msg) {
		switch (msg.what) {
		// 交接记录
		case MsgID.trade_record:
			onRefreshComplete();
			final ReqResult<TradeRecordResultBean> result = JSON.parser(msg.obj, TradeRecordResultBean.class);
			if (checkLoginStatus(result) && result.getResult() != null) {
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						TradeRecordResultBean items = result.getResult();
						mAdapter = new TradeRecordAdapter(items.getJoinList(), getContext());
						mTradeRecordNum.setText("" + items.getParcelCount());
						mTradeRecordCount.setText("共" + items.getParcelCount() + "个包裹");
						mTradeRecordContent.setText("累计交易" + items.getJoinCount() + "次");
						mListView.setAdapter(mAdapter);
						setListViewListener();
					}
				});
			} else {
				showToast(result.getMessage());
			}
			return true;
		default:
			return super.handleMessage(msg);
		}
	}

	protected void setListViewListener() {
		mListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent intent = new Intent();
				intent.setClass(getContext(), TradeFinishActivity.class);
				intent.putExtra("item", mAdapter.getItem(position));
				startActivity(intent);
			}
		});

	}
}
